package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


/**
 * @author mramsey3
 *
 */
public class AddAppointmentDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	
	private JPanel titlePanel;
	
	private JPanel centerPanel;
	
	private JPanel firstNamePanel;
	private JTextField firstNameTextField;
	private JButton firstNameEditButton;
	private JPanel lastNamePanel;
	private JTextField lastNameTextField;
	private JButton lastNameEditButton;
	private JPanel reasonPanel;
	private JTextField reasonTextField;
	private JButton reasonEditButton;
	private JPanel doctorPanel;
	private JComboBox doctorComboBox;
	private JPanel timePanel;
	private JComboBox timeComboBox;
	
	private JPanel buttonPanel;
	private JButton requestButton;
	private JButton cancelButton;
	
	/**
	 * 
	 */
	public AddAppointmentDialog() {
	    setSize(new Dimension(400, 300));
	    setLocation(50, 50);
	    setLayout(new BorderLayout());
	    
	    add(getTitlePanel(), BorderLayout.NORTH);
	    
	    add(getCenterPanel(), BorderLayout.CENTER);
	}
	
	/**
	 * @return
	 */
	private JPanel getTitlePanel() {
	    if (titlePanel == null) {
		titlePanel = new JPanel() {
		    private static final long serialVersionUID = 1L;
		    
		    public void paintComponent(Graphics g) {
			super.paintComponent(g);
                        g.setColor(new Color(100, 150, 220));
                        g.fillRect(0, 0, getWidth(), getHeight());
                        g.setColor(Color.white);
                        g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
                        g.drawString("add appointment:", 24, getHeight() - 8);
		    }
		};
		titlePanel.setPreferredSize(new Dimension(400, 60));
	    }
	    return titlePanel;
	}
	
	/**
	 * @return
	 */
	private JPanel getCenterPanel() {
	    if (centerPanel == null) {
		centerPanel = new JPanel();
	
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
		
		centerPanel.add(getFirstNamePanel());
		centerPanel.add(getLastNamePanel());
		centerPanel.add(getReasonPanel());
		centerPanel.add(getDoctorPanel());
		centerPanel.add(getTimePanel());
		centerPanel.add(getButtonPanel());
	    }
	    return centerPanel;
	}
	
	/**
	 * @return
	 */
	private JPanel getFirstNamePanel() {
	    if (firstNamePanel == null) {
		firstNamePanel = new JPanel();
		firstNamePanel.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("  First Name:");
		label.setPreferredSize(new Dimension(80, 25));
		firstNamePanel.add(label, BorderLayout.WEST);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.anchor = GridBagConstraints.WEST;
		gbc1.weightx = 1;
		centerPanel.add(getFirstNameTextField(), gbc1);
		firstNamePanel.add(centerPanel, BorderLayout.CENTER);
		
		JPanel rightPanel = new JPanel();
		rightPanel.setPreferredSize(new Dimension(60, 25));
		rightPanel.setLayout(new GridBagLayout());
		rightPanel.add(getFirstNameEditButton());
		firstNamePanel.add(rightPanel, BorderLayout.EAST);
	    }
	    return firstNamePanel;
	}
	
	/**
	 * @return
	 */
	private JTextField getFirstNameTextField() {
	    if (firstNameTextField == null) {
		firstNameTextField = new JTextField();
		firstNameTextField.setText("Random");
		firstNameTextField.setPreferredSize(new Dimension(170, 20));
		firstNameTextField.setEditable(false);
	    }
	    return firstNameTextField;
	}
	
	/**
	 * @return
	 */
	private JButton getFirstNameEditButton() {
	    if (firstNameEditButton == null) {
		firstNameEditButton = new JButton("edit");
		firstNameEditButton.addActionListener(this);
		firstNameEditButton.setMargin(new Insets(0, 0, 0, 0));
		firstNameEditButton.setPreferredSize(new Dimension(50, 20));
	    }
	    return firstNameEditButton;
	}
	
	/**
	 * @return
	 */
	private JPanel getLastNamePanel() {
	    if (lastNamePanel == null) {
		lastNamePanel = new JPanel();
		lastNamePanel.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("  Last Name:");
		label.setPreferredSize(new Dimension(80, 25));
		lastNamePanel.add(label, BorderLayout.WEST);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.anchor = GridBagConstraints.WEST;
		gbc1.weightx = 1;
		centerPanel.add(getLastNameTextField(), gbc1);
		lastNamePanel.add(centerPanel, BorderLayout.CENTER);
		
		JPanel rightPanel = new JPanel();
		rightPanel.setPreferredSize(new Dimension(60, 25));
		rightPanel.setLayout(new GridBagLayout());
		rightPanel.add(getLastNameEditButton());
		lastNamePanel.add(rightPanel, BorderLayout.EAST);
	    }
	    return lastNamePanel;
	}
	
	/**
	 * @return
	 */
	private JTextField getLastNameTextField() {
	    if (lastNameTextField == null) {
		lastNameTextField = new JTextField();
		lastNameTextField.setText("Random");
		lastNameTextField.setPreferredSize(new Dimension(170, 20));
		lastNameTextField.setEditable(false);
	    }
	    return lastNameTextField;
	}
	
	/**
	 * @return
	 */
	private JButton getLastNameEditButton() {
	    if (lastNameEditButton == null) {
		lastNameEditButton = new JButton("edit");
		lastNameEditButton.addActionListener(this);
		lastNameEditButton.setMargin(new Insets(0, 0, 0, 0));
		lastNameEditButton.setPreferredSize(new Dimension(50, 20));
	    }
	    return lastNameEditButton;
	}
	
	/**
	 * @return
	 */
	private JPanel getReasonPanel() {
	    if (reasonPanel == null) {
		reasonPanel = new JPanel();
		reasonPanel.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("  Reason:");
		label.setPreferredSize(new Dimension(80, 25));
		reasonPanel.add(label, BorderLayout.WEST);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.anchor = GridBagConstraints.WEST;
		gbc1.weightx = 1;
		centerPanel.add(getReasonTextField(), gbc1);
		reasonPanel.add(centerPanel, BorderLayout.CENTER);
		
		JPanel rightPanel = new JPanel();
		rightPanel.setPreferredSize(new Dimension(60, 25));
		rightPanel.setLayout(new GridBagLayout());
		rightPanel.add(getReasonEditButton());
		reasonPanel.add(rightPanel, BorderLayout.EAST);
	    }
	    return reasonPanel;
	}
	
	/**
	 * @return
	 */
	private JTextField getReasonTextField() {
	    if (reasonTextField == null) {
		reasonTextField = new JTextField();
		reasonTextField.setText("Too many all nighters coding cs");
		reasonTextField.setPreferredSize(new Dimension(170, 20));
		reasonTextField.setEditable(false);
	    }
	    return reasonTextField;
	}
	
	/**
	 * @return
	 */
	private JButton getReasonEditButton() {
	    if (reasonEditButton == null) {
		reasonEditButton = new JButton("edit");
		reasonEditButton.addActionListener(this);
		reasonEditButton.setMargin(new Insets(0, 0, 0, 0));
		reasonEditButton.setPreferredSize(new Dimension(50, 20));
	    }
	    return reasonEditButton;
	}
	
	/**
	 * @return
	 */
	private JPanel getDoctorPanel() {
	    if (doctorPanel == null) {
		doctorPanel = new JPanel();
		doctorPanel.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("  Reason:");
		label.setPreferredSize(new Dimension(80, 25));
		doctorPanel.add(label, BorderLayout.WEST);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.anchor = GridBagConstraints.WEST;
		gbc1.weightx = 1;
		centerPanel.add(getDoctorComboBox(), gbc1);
		doctorPanel.add(centerPanel, BorderLayout.CENTER);
	    }
	    return doctorPanel;
	}
	
	/**
	 * @return
	 */
	private JComboBox getDoctorComboBox() {
	    if (doctorComboBox == null) {
		String[] docs = {"Dr. 1", "Dr. 2" , "Dr. 3", "Dr. 4", "Dr. 5",
			"Dr. 6", "Dr. 7" , "Dr. 8", "Dr. 9", "Dr. 10"};
		doctorComboBox = new JComboBox(docs);
		doctorComboBox.setPreferredSize(new Dimension(170, 20));
	    }
	    return doctorComboBox;
	}
	
	/**
	 * @return
	 */
	private JPanel getTimePanel() {
	    if (timePanel == null) {
		timePanel = new JPanel();
		timePanel.setLayout(new BorderLayout());
		
		JLabel label = new JLabel("  Reason:");
		label.setPreferredSize(new Dimension(80, 25));
		timePanel.add(label, BorderLayout.WEST);
		
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.anchor = GridBagConstraints.WEST;
		gbc1.weightx = 1;
		centerPanel.add(getTimeComboBox(), gbc1);
		timePanel.add(centerPanel, BorderLayout.CENTER);
	    }
	    return timePanel;
	}
	
	/**
	 * @return
	 */
	private JComboBox getTimeComboBox() {
	    if (timeComboBox == null) {
		String[] times = {"8:00 AM", "9:00 AM" , "10:00 AM", "11:00 AM", 
			"1:00 PM", "2:00 PM" , "3:00 PM", "4:00 PM"};
		timeComboBox = new JComboBox(times);
		timeComboBox.setPreferredSize(new Dimension(170, 20));
	    }
	    return timeComboBox;
	}
	
	/**
	 * @return
	 */
	private JPanel getButtonPanel() {
	    if (buttonPanel == null) {
		buttonPanel = new JPanel();
		buttonPanel.setLayout(new GridBagLayout());
		
		buttonPanel.add(getRequestButton());
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(getCancelButton());
	    }
	    return buttonPanel;
	}
	
	/**
	 * @return
	 */
	private JButton getRequestButton() {
	    if (requestButton == null) {
		requestButton = new JButton("Request");
		requestButton.addActionListener(this);
		requestButton.setPreferredSize(new Dimension(80, 30));
	    }
	    return requestButton;
	}
	
	/**
	 * @return
	 */
	private JButton getCancelButton() {
	    if (cancelButton == null) {
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		cancelButton.setPreferredSize(new Dimension(80, 30));
	    }
	    return cancelButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
	    if (e.getSource() == getFirstNameEditButton()) {
		String newFirstName = JOptionPane.showInputDialog("First name:");
		getFirstNameTextField().setText(newFirstName);
	    } else if (e.getSource() == getLastNameEditButton()) {
		String newLastName = JOptionPane.showInputDialog("Last name:");
		getLastNameTextField().setText(newLastName);
	    } else if (e.getSource() == getReasonEditButton()) {
		String newReason= JOptionPane.showInputDialog("Reason:");
		getReasonTextField().setText(newReason);
	    } else if (e.getSource() == getRequestButton()) {
		setVisible(false);
	    } else if (e.getSource() == getCancelButton()) {
		setVisible(false);
	    }
	}
}
